package cn.iocoder.yudao.module.vote.service.voterecorddetail;

import cn.iocoder.yudao.module.vote.controller.admin.voteuserresult.vo.VoteOnlineUpdateReqVO;
import cn.iocoder.yudao.module.vote.utils.VoteStatusUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.vote.controller.admin.voterecorddetail.vo.*;
import cn.iocoder.yudao.module.vote.dal.dataobject.voterecorddetail.VoteRecordDetailDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.vote.convert.voterecorddetail.VoteRecordDetailConvert;
import cn.iocoder.yudao.module.vote.dal.mysql.voterecorddetail.VoteRecordDetailMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.vote.enums.ErrorCodeConstants.*;

/**
 * 投票记录详情 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class VoteRecordDetailServiceImpl extends ServiceImpl<VoteRecordDetailMapper,VoteRecordDetailDO> implements VoteRecordDetailService {

    @Resource
    private VoteRecordDetailMapper recordDetailMapper;

    @Override
    public Integer createRecordDetail(VoteRecordDetailCreateReqVO createReqVO) {
        // 插入
        VoteRecordDetailDO recordDetail = VoteRecordDetailConvert.INSTANCE.convert(createReqVO);
        recordDetailMapper.insert(recordDetail);
        // 返回
        return recordDetail.getId();
    }

    @Override
    public void updateRecordDetail(VoteRecordDetailUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateRecordDetailExists(updateReqVO.getId());
        // 更新
        VoteRecordDetailDO updateObj = VoteRecordDetailConvert.INSTANCE.convert(updateReqVO);
        recordDetailMapper.updateById(updateObj);
    }

    @Override
    public void deleteRecordDetail(Integer id) {
        // 校验存在
        this.validateRecordDetailExists(id);
        // 删除
        recordDetailMapper.deleteById(id);
    }

    private void validateRecordDetailExists(Integer id) {
        if (recordDetailMapper.selectById(id) == null) {
            throw exception(RECORD_DETAIL_NOT_EXISTS);
        }
    }

    @Override
    public VoteRecordDetailDO getRecordDetail(Integer id) {
        return recordDetailMapper.selectById(id);
    }

    @Override
    public List<VoteRecordDetailDO> getRecordDetailList(Collection<Integer> ids) {
        return recordDetailMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<VoteRecordDetailDO> getRecordDetailPage(VoteRecordDetailPageReqVO pageReqVO) {
        return recordDetailMapper.selectPage(pageReqVO);
    }

    @Override
    public List<VoteRecordDetailDO> getRecordDetailList(VoteRecordDetailExportReqVO exportReqVO) {
        return recordDetailMapper.selectList(exportReqVO);
    }

    /**
     * 通过投票id删除投票记录
     * @param voteId
     * @return
     */
    @Override
    public int deleteByVoteId(Integer voteId){
        return recordDetailMapper.deleteByVoteId(voteId);
    }


    /**
     * 批量插入
     * @param onlineReq
     * @param recordId
     */
    @Override
    public void insertBatchVote(VoteOnlineUpdateReqVO onlineReq,Integer recordId){
        List<VoteRecordDetailDO> saveList = new ArrayList<>();
        onlineReq.getVoteStatusList().forEach(voteOnline->{
            VoteRecordDetailDO resultDO = new VoteRecordDetailDO();
            resultDO.setIp(onlineReq.getIp());
            resultDO.setVoteId(onlineReq.getVoteId());
            resultDO.setRecordId(recordId);
            resultDO.setUserResultId(voteOnline.getId());
            resultDO.setVoteName(onlineReq.getVoteName());
            resultDO.setUserName(voteOnline.getUserName());
            resultDO.setVoteType(onlineReq.getVoteType());
            resultDO.setQuantifiedScore(voteOnline.getQuantifiedScore());
            resultDO.setUserNo(voteOnline.getUserNo());
            resultDO.setTenantId(onlineReq.getTenantId());
            resultDO.setKeyTasks(voteOnline.getKeyTasks());
            resultDO.setProgress(voteOnline.getProgress());
            resultDO.setCollaboration(voteOnline.getCollaboration());
            resultDO.setTeamBuilding(voteOnline.getTeamBuilding());
            resultDO.setVoteReviewerType(voteOnline.getVoteReviewerType());
            saveList.add(resultDO);
        });
        saveBatch(saveList);
    }

    @Override
    public List<VoteRecordCountVO> getByResultId(Integer voteId, Integer userResultId, Integer votePersonType){
        return recordDetailMapper.getByResultId(voteId,userResultId,votePersonType);
    }


    /**
     * 获取投票详情
     * @param voteId
     * @return
     */
    public List<VoteRecordDetailVO> getByVoteId(Integer voteId){
        return recordDetailMapper.getByVoteId(voteId);
    }

    public static void main(String[] args) {
        String data = "http://10.105.23.25/admin-api/infra/file/5/get/开发规范(1)20221010181134359.pdf";
        String sub = "/admin-api";
        int startIndex = data.indexOf(sub);
        System.out.println(data.substring(startIndex+data.length()));
    }
}
